package leetcode.listnode;

import leetcode.ListNode;

/**
 * @author Cheng Jun
 * Description: 给定一个已排序的链表的头 head ， 删除所有重复的元素，使每个元素只出现一次 。返回 已排序的链表
 * https://leetcode.cn/problems/remove-duplicates-from-sorted-list/
 * @version 1.0
 * @date 2022/7/26 21:39
 */
public class deleteDuplicates1 {

    /**
     * 当前节点和 下一个节点作比较
     * @author Cheng Jun
     * @date 2022/7/26 21:45
     * @param head
     * @return leetcode.ListNode
     */
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode dummy = new ListNode(Integer.MAX_VALUE, head);
        ListNode preNode = dummy;
        ListNode nextNode = dummy.next;
        while(nextNode != null){
            if( preNode.val != nextNode.val){
                preNode = nextNode;
                nextNode = nextNode.next;
            }else{
                preNode.next = nextNode.next;
                nextNode = preNode.next;
            }
        }
        return dummy.next;
    }
}
